#include "DBAccess.h"

using namespace std;

sqlite3 *DBAccess::database = NULL;

string DBAccess::GetItemName(int id)
{
	string itemName = "";
	if (database != NULL || sqlite3_open(ITEMSDB, &database) == SQLITE_OK) {
		const char *sqlQuery = "SELECT Name, Watch FROM Items WHERE Id = ? ";
		sqlite3_stmt *selectstmt;
		if(sqlite3_prepare_v2(database, sqlQuery, -1, &selectstmt, NULL) == SQLITE_OK) {

			sqlite3_bind_int(selectstmt, 1, id);

			if (sqlite3_step(selectstmt) == SQLITE_ROW) {
				if(sqlite3_column_int(selectstmt, 1) > 0)
					itemName = (const char*)(sqlite3_column_text(selectstmt, 0));
				
			}
		}
		sqlite3_finalize(selectstmt);
	} else {
		//Even though the open call failed, close the database connection to release all the memory.
		Utils::Log(LOGFILE, sqlite3_errmsg(database));
		Utils::Log(LOGFILE, "\n");

		sqlite3_close(database); 
	}
	

	return itemName;
}